package com.zaribagames.fxunitautomation
{
	import flash.filesystem.File;

	public class Settings
	{
	
		public static const PATH_PARAM_NAME:String = "path";
		
		public static var PATH:String;
		public static var DIR:File;
		public static var CONFIG:File;
		public static var FXUNIT_SETTINGS_PATH:File;
		
		private static var mArguments:Array;
		
		public function Settings()
		{
			
		}
		
		public static function init(arguments:Array):String{
			var msg:String;
			mArguments = arguments;
			
			if((msg = initDir())!="")
				return msg;
			
			if((msg = initConfigFile())!="")
				return msg;
			
			if((msg = initFxUnitDir())!="")
				return msg;
			
			return "";			
		}
		
		private static function initDir():String{
			var pathArgument:String;
			
			for each(var argument:String in mArguments){
				if(argument.indexOf(PATH_PARAM_NAME+"=")==0){
					pathArgument = argument;
					break;
				}
			}
			
			if(pathArgument==null){
				return "No 'path' argument";
			}
			
			pathArgument = pathArgument.substr((PATH_PARAM_NAME+"=").length);
			
			if(pathArgument.lastIndexOf("/") != -1 ){
				pathArgument = pathArgument.substr(0,pathArgument.length-1);
			}
			
			if(pathArgument.lastIndexOf("\\") != -1 ){
				pathArgument = pathArgument.substr(0,pathArgument.length-1);
			}
			
			PATH = pathArgument + "/src/flexUnitTests";
			
			var dir:File = new File( PATH );
			
			if( !dir.exists || !dir.isDirectory ){
				return "Supplied path does not exists";
			}
			
			return "";
		}
		
		private static function initFxUnitDir():String{
			var fxdir:File = new File( PATH + "/../../.FlexUnitSettings" );
			
			if( !fxdir.exists || !fxdir.isDirectory ){
				return "Flex Unit Settings dir does not exists";
			}
			
			FXUNIT_SETTINGS_PATH = fxdir;
			return "";
		}
		
		private static function initConfigFile():String{
			var config:File = new File( PATH + "/config.xml" );
			
			if( !config.exists ){
				return "Config file doesn't exists";
			}
			
			CONFIG = config;
			return "";
		}
	}
}